const Service = require('egg').Service;

class MemberService extends Service {

	async info(user_base, company_code) {

		const result = await this.app.mysql.get('weshop').query(
			'select nickname, headimgurl ' +
			'from qm_user_wx ' +
			'where user_base = ? and company_code = ? ', [user_base, company_code]
		);

		console.log(result);

		return result[0];
	}


	async create(para) {
		const { openid, nickname, headimgurl, user_base, wxid, company_code} = para;
		const sql = this.app.mysql.get('weshop');
		const result = await sql.query(
			'insert into qm_user_wx ' +
			'(id, openid, nickname, headimgurl, user_base, wxid, create_time, company_code) ' + 
			'VALUES ' + 
			'(UUID(), ?, ?, ?, ?, ?, NOW(), ?)', [openid, nickname, headimgurl, user_base, wxid, company_code]
		);

		console.log(result);

		return result.affectedRows === 1;
	}


	async update (para) {
		const { nickname, headimgurl, user_base, company_code } = para;
		const sql = this.app.mysql.get('weshop');
		const result = await sql.query(
			'update qm_user_wx ' +
			'set nickname=?, headimgurl=? ' + 
			'where user_base = ? and company_code = ? ', [nickname, headimgurl, user_base, company_code]
		);

		return result.affectedRows === 1;
	}


	async redisInfo (uuid, what) {
		const member = await this.app.redis.get('member').hget('juyou_ticket_' + uuid, what);
		return member;
	}



	async info111(user_base, company_code) {

		const result = await this.app.mysql.get('weshop').query(
			'select nickname11, headimgurl ' +
			'from qm_user_wx ' +
			'where user_base = ? and company_code = ? ', [user_base, company_code]
		);

		console.log(result);

		return result[0];
	}
  	
}

module.exports = MemberService;

